package client;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * A class which creates a message digest from the username and password.
 * @author ic08jh7
 */
public class MakeDigest {
/**
 * Creates a message digester.
 */
	public MakeDigest() {
	}
/**
 * Creates a MD5 hash from the given passwd and salt.
 * @param passwd
 * 				The passwd to create a hash from.
 * @param salt
 * 				The salt to the passwd.
 * @return
 * 			The hash created from the passwd and salt.
 */
	public String makeHash(String passwd, String salt) {
		byte[] passwdBytes = passwd.getBytes();
		byte[] hashBytes = salt.getBytes();

		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(passwdBytes);
			algorithm.update(hashBytes);
			byte messageDigest[] = algorithm.digest();

			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < messageDigest.length; i++) {
				hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
			}
			String hash = hexString + "";
			return hash;
		} catch (NoSuchAlgorithmException nsae) {

		}
		return null;
	}
}
